import os
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class Login_Operation:
    #启动浏览器chrome
    @pytest.fixture()
    def driver(self):
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.maximize_window()
        yield driver
        driver.quit()

    #定义登录方法
    def login(self, driver, username, password):
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 获取结果——网页标题
        result = driver.title
        return result

    def assert_title(self, result, driver):
        expect = ["个人理财系统", "后台管理系统"]
        if result in expect:


            print("登录成功")
        else:
            print("登录失败")
            try:
                os.mkdir("false")
            except Exception as e:
                print(e)
            driver.save_screenshot("false/login.jpg")
        assert result in expect

    #既然如此咱们再搞一个注册类
class make_user:
    @pytest.fixture()
    def driver(self):
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.maximize_window()
        yield driver
        driver.quit()
    def create_user(self,driver,username,password,repassword):
        # 输入注册的用户名与密码
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        driver.find_element(By.XPATH, "//input[@id='repassword']").send_keys(repassword)
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        #为了断言: 获取结果——网页标题
        result = driver.title
        return result

    #再定义一个注册的断言,注册成功后,会跳转登录页面,此时获取页面标题
    def assert_register(self,driver,result):
        expect = '@@@@@度小满理财-登录[内测版]'
        if result == expect:
            print("注册成功")
        else:
            print("注册失败")
            # try:
            #     os.mkdir("false")
            # except Exception as e:
            #     print(e)
            driver.save_screenshot("false/register.jpg")
        assert result in expect

#我们写一个银行卡操作的类
class Bankcard:
    @pytest.fixture()
    def driver(self):
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.maximize_window()
        yield driver
        driver.quit()
    def add(self, driver, bank_name, card_id, sort):
        # 点击新增
        driver.find_element(By.XPATH, '//*[@id="bankCard_add_modal_btn"]').click()
        # 输入银行名称
        driver.find_element(By.XPATH, '//input[@id="cardBank_add_input"]').send_keys(bank_name)
        # 选择银行卡类别 sort=1为借记卡，sort=2为信用卡
        driver.find_element(By.XPATH, '//*[@id="bankCardAddModal"]'
                                      f'/div/div/div[2]/form/div[2]/div[1]/div/label[{sort}]/span').click()
        # 输入16位正常卡号
        driver.find_element(By.XPATH, '//input[@id="cardNum_add_input"]').send_keys(card_id)
        # 点击确认
        driver.find_element(By.XPATH, '//*[@id="bankCard_save_btn"]').click()
        time.sleep(0.5)

    def delete(self, driver, card_id, choice):
        # 点击选择其中一个银行卡，点击删除
        driver.find_element(By.XPATH, '//button[@class="btn btn-danger delete_btn" and '
                                      f'@delete-name="{card_id}"]').click()
        driver.find_element(By.XPATH, f'//button[text()="{choice}"]').click()
        time.sleep(0.5)

    def select_mysql_add(self, bank_name, card_id, username, db):
        result = db.select(sql="SELECT * FROM bankcard JOIN user ON `bankcard`.userId = user.id WHERE "
                               f"cardBank = '{bank_name}' AND cardNum = '{card_id}' AND username = '{username}';")
        return result
    def select_mysql_delete(self, card_id, username, db):
        result = db.select(sql="SELECT * FROM bankcard JOIN user ON `bankcard`.userId = `user`.id WHERE "
                           f"cardNum = '{card_id}' AND username = '{username}';")
        return result

    def assert_add(self, result, driver):
        if result is not ():
            print('添加成功')
        else:
            print('添加失败')
            try:
                os.mkdir("false")

            except Exception as e:
                print(e)
            driver.save_screenshot("false/add.jpg")

        assert result is not ()

    def assert_delete(self, result, driver):
        if result is ():
            print('删除成功')
        else:
            print('删除失败')
            try:
                os.mkdir("false")
            except Exception as e:
                print(e)
            driver.save_screenshot("false/delete.jpg")
        assert result is ()

#写一个买入基金类
class buy:
    @pytest.fixture()
    def driver(self):
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.maximize_window()
        yield driver
        driver.quit()
    def buyme(self,driver,paycode):
        # el = driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/a").click()
        # time.sleep(2)
        # el = driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/ul/li[1]/a").click()
        # time.sleep(2)
        el = driver.find_element(By.XPATH, "//button[@buybtnname='支付宝零钱理财']").click()
        time.sleep(2)
        el = driver.find_element(By.XPATH,"//*[@class='layui-layer-content']/input[@id='password']").send_keys(paycode)
        el = driver.find_element(By.PARTIAL_LINK_TEXT, '确定').click()
        current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        time.sleep(0.5)
        return current_time

    def select_mysql_buy(self, current_time, db):
        result = db.select(sql="SELECT * FROM flow_of_funds JOIN user ON flow_of_funds.userId = user.id WHERE "
                           f"source = '支付宝零钱理财' AND createTime = '{current_time}';")
        return result

    def assertbuyme(self,result,driver):
            if result is not ():
                print('购买成功')

            else:
                print('购买失败')
                try:
                    os.mkdir("false")

                except Exception as e:
                    print(e)
                driver.save_screenshot("false/buy.jpg")

            assert result is not ()

#再写一个申请网贷类
class dept:
    def buydept(self,driver,money,day):
        driver.find_element(By.XPATH,"//input[@id= 'amount']").send_keys(money)
        time.sleep(2)
        driver.find_element(By.XPATH,'//input[@id= "term"]').send_keys(day)
        time.sleep(3)
        el = driver.find_element(By.ID, 'submit').click()
        time.sleep(3)
        el = driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(2)
        current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        time.sleep(2)
        return current_time

    def select_my_buydept(self,current_time,db):
        result = db.select("SELECT * FROM loan JOIN USER ON loan.userId = user.id WHERE "
                           f"startTime = '{current_time}';")
        return result

    def assert_loan(self, result, driver):
        if result is not ():
            print('贷款成功')
        else:
            print('贷款失败')
            try:
                os.mkdir("false")
            except Exception as e:
                print(e)
            driver.save_screenshot("false/loan.jpg")

        assert result is not ()
